package cn.xspace.app.filemg.view;

import android.app.Activity;
import android.content.Context;
import android.os.AsyncTask;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;

import com.xspace.android.xafcommon.util.BaseRProxy;

import java.util.ArrayList;

import cn.xspace.app.filemg.bean.ImgPackInfo;
import cn.xspace.app.filemg.util.FileMg;

public class FileImgPackAdapter extends BaseAdapter {
	private Context context;
	private ArrayList<ImgPackInfo> list;
	private CallBackChecked mCallBack;

	public FileImgPackAdapter(Context context, ArrayList<ImgPackInfo> objects,
			CallBackChecked mCallBack) {
		super();
		this.context = context;
		this.mCallBack = mCallBack;
		list = objects;
	}

	public interface CallBackChecked {
		public void isChecked(int position);
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return list.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return list.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		ViewHolder viewHolder;
		if (convertView == null) {
			LayoutInflater inflater = ((Activity) context).getLayoutInflater();
			convertView = inflater.inflate(BaseRProxy.img_pack_item, null);
			viewHolder = new ViewHolder(convertView);
		
			convertView.setTag(viewHolder);
		} else {
			viewHolder = (ViewHolder) convertView.getTag();
		}
		final ImgPackInfo info = list.get(position);
		viewHolder.mName.setText(info.getPackName());

		if (info.getIoc() == null || "".equals(info.getIoc())) {
			info.setIoc(FileMg.getImageThumbnail(info.getIocPath()));
			viewHolder.mIoc.setBackgroundDrawable(info.getIoc());
		} else {
			viewHolder.mIoc.setBackgroundDrawable(info.getIoc());
		}
	    viewHolder.mCheckBox.setChecked(info.isCheckFlag());
		viewHolder.mCheckBox.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				mCallBack.isChecked(position);
			}
		});
		return convertView;
	}

	private Handler mHandler = new Handler();

	private class MyAsyncTask extends AsyncTask {
		@Override
		protected Object doInBackground(final Object... arg0) {
			// TODO Auto-generated method stub
			ImgPackInfo info = (ImgPackInfo) arg0[0];
			info.setIoc(FileMg.getImageThumbnail(info.getIocPath()));

			return null;
		}

	}

	class ViewHolder {
		private TextView mName;
		private ImageView mIoc;
		public CheckBox mCheckBox;
		public ViewHolder(View v){
			mCheckBox = (CheckBox) v.findViewById(BaseRProxy.box);
			mIoc = (ImageView) v.findViewById(BaseRProxy.picture);
			mName = (TextView) v.findViewById(BaseRProxy.img_pack_name);
		}
	}
}
